/*
 * Copyright 2010 Laurent Moulinier, Christian Matzat and others
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

package com.gafmedia.graph;

import android.content.Context;
import android.graphics.*;
import android.graphics.BitmapFactory.Options;
import android.util.AttributeSet;

import java.util.List;

/**
 * View that renders a pie chart.
 * <p/>
 * based on code from GAFMEDIA Studio, initiated by Laurent Moulinier
 * <a href="http://blog.gafmediastudio.com/2010/04/01/draw-a-pie-chart-with-android/"></a>
 */
public class PieChartView extends BaseChartView {

    private static final String TAG = BASE_TAG + PieChartView.class.getSimpleName();

    static final float START_INC = 30;

    private int overlayId;
    private int maxConnection;
    private boolean threeDimensionEffect;
    private List<PieChartItem> data;

    public PieChartView(Context context) {
        super(context);
    }

    public PieChartView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        if (state != IS_READY_TO_DRAW) return;
        canvas.drawColor(backgroundColor);

        Paint backgroundPaints = new Paint();
        backgroundPaints.setAntiAlias(true);
        backgroundPaints.setStyle(Paint.Style.FILL);
        backgroundPaints.setColor(0x88FF0000);
        backgroundPaints.setStrokeWidth(0.5f);

        Paint linePaints = new Paint();
        linePaints.setAntiAlias(true);
        linePaints.setStyle(Paint.Style.STROKE);
        linePaints.setColor(0xff000000);
        linePaints.setStrokeWidth(0.5f);

        RectF ovals;
        if (threeDimensionEffect) {
            Paint threeDimensionEffectPaints = new Paint();
            threeDimensionEffectPaints.setAntiAlias(true);
            threeDimensionEffectPaints.setStyle(Paint.Style.STROKE);
            threeDimensionEffectPaints.setColor(Color.WHITE);
            threeDimensionEffectPaints.setStrokeWidth(0.5f);
            for (int i = 1; i <= 2; i++) {
                ovals = new RectF(gapLeft, gapTop, (width - gapRight) + 1, (height - gapBottom) + i);
                canvas.drawOval(ovals, threeDimensionEffectPaints);
            }
        }

        ovals = new RectF(gapLeft, gapTop, width - gapRight, height - gapBottom);

        float start = START_INC;
        for (PieChartItem pieChartItem : data) {
            backgroundPaints.setColor(pieChartItem.color);
            float sweep = 360f * ((float) pieChartItem.count / (float) maxConnection);
            canvas.drawArc(ovals, start, sweep, true, backgroundPaints);
            canvas.drawArc(ovals, start, sweep, true, linePaints);
            start += sweep;
        }

        Options options = new BitmapFactory.Options();
        options.inScaled = false;
        options.outWidth = width;
        if (overlayId != 0) {
            Bitmap overlayBitmap = BitmapFactory.decodeResource(getResources(), overlayId, options);
            canvas.drawBitmap(overlayBitmap, gapLeft, gapTop, null);
        }
        state = IS_DRAWN;
    }

    /**
     * Sets the geometry of this chart.
     *
     * @param width                the width
     * @param height               the height
     * @param gapLeft              gap from left
     * @param gapRight             gap from right
     * @param gapTop               gap from top
     * @param gapBottom            gap from bottom
     * @param overlayId            an image id to lay over the chart
     * @param threeDimensionEffect switches the three dimension effect of the pie
     */
    public void setGeometry(int width, int height, int gapLeft, int gapRight, int gapTop, int gapBottom, int overlayId, boolean threeDimensionEffect) {
        this.width = width;
        this.height = height;
        this.gapLeft = gapLeft;
        this.gapRight = gapRight;
        this.gapTop = gapTop;
        this.gapBottom = gapBottom;
        this.overlayId = overlayId;
        this.threeDimensionEffect = true;
    }

    /**
     * Sets the paramters that define how the pie chart should be displayed
     *
     * @param backgroundColor color of the background
     */
    public void setSkinParams(int backgroundColor) {
        this.backgroundColor = backgroundColor;
    }

    public void setData(List<PieChartItem> data, int maxConnection) {
        this.data = data;
        this.maxConnection = maxConnection;
        state = IS_READY_TO_DRAW;
    }
}